#!/bin/bash
if [[ $(whoami) != "root" ]]; then
	echo "Please login as user root";
	exit 1;
fi
if [[ $LFS == "" || ! -d $LFS ]]; then
	echo "LFS variable not initialized... abort";
	exit 2;
fi

# get target name
LFS_TGT="$(uname -m)-$(cat $LFS/sources/distname | tr -d '[:space:]')" || exit 3;

# release already mounted points
$LFS/sources/prepare/unmount_chroot.sh

# mounting virtual kernel file system
mkdir -pv $LFS/{dev,proc,sys,run}
if [ ! -c $LFS/dev/console ]; then
	mknod -m 600 $LFS/dev/console c 5 1 || exit 1
fi
if [ ! -c $LFS/dev/null ]; then
	mknod -m 666 $LFS/dev/null c 1 3  || exit 1
fi
DFA="$(df -a)";
if [[ $(echo "$DFA" | grep "$LFS/dev$") == "" ]]; then
	mount -v --bind /dev $LFS/dev || exit 1
fi
if [[ $(echo "$DFA" | grep "$LFS/dev/pts$") == "" ]]; then
	mount -vt devpts devpts $LFS/dev/pts -o gid=5,mode=620 || exit 1
fi
if [[ $(echo "$DFA" | grep "$LFS/proc$") == "" ]]; then
	mount -vt proc proc $LFS/proc || exit 1
fi
if [[ $(echo "$DFA" | grep "$LFS/sys$") == "" ]]; then
	mount -vt sysfs sysfs $LFS/sys || exit 1
fi
if [[ $(echo "$DFA" | grep "$LFS/run$") == "" ]]; then
	mount -vt tmpfs tmpfs $LFS/run || exit 1
fi
if [ -h $LFS/dev/shm ]; then
  mkdir -pv $LFS/$(readlink $LFS/dev/shm)  || exit 1
fi

# update source packages
wget -nc -i wget-list -P $LFS/sources;

# initializing directories
mkdir -pv $LFS/{bin,boot,etc/{opt,sysconfig},home,lib,mnt,opt}
mkdir -pv $LFS/{media/{floppy,cdrom},sbin,srv,var}
install -dv -m 0750 $LFS/root
install -dv -m 1777 $LFS/tmp $LFS/var/tmp
mkdir -pv $LFS/usr/{,local/}{bin,include,lib,sbin,src}
mkdir -pv $LFS/usr/{,local/}share/{color,dict,doc,info,locale,man}
mkdir -pv  $LFS/usr/{,local/}share/{misc,terminfo,zoneinfo}
mkdir -pv  $LFS/usr/libexec
mkdir -pv $LFS/usr/{,local/}share/man/man{1..8}
mkdir -pv $LFS/lib/{firmware,udev}
mkdir -pv $LFS/lib/udev/rules.d
mkdir -pv $LFS/etc/udev/rules.d

case $(uname -m) in
 x86_64) ln -sfv lib $LFS/lib64
         ln -sfv lib $LFS/usr/lib64
         ln -sfv lib $LFS/usr/local/lib64 ;;
esac

mkdir -pv $LFS/var/{log,mail,spool}
ln -sfv /run $LFS/var/run
ln -sfv /run/lock $LFS/var/lock
mkdir -pv $LFS/var/{opt,cache,lib/{color,misc,locate},local}

# folder for package archive
mkdir -pv $LFS/usr/pkg

# make binary links
[ -e $LFS/bin/bash ] || ln -sfv /tools/bin/{bash,cat,echo,pwd,stty} $LFS/bin;
[ -e $LFS/usr/bin/perl ] || ln -sfv /tools/bin/perl $LFS/usr/bin;
[ -e $LFS/usr/lib/libgcc_s.so ] || ln -sfv /tools/lib/libgcc_s.so{,.1} $LFS/usr/lib
[ -e $LFS/usr/lib/libstdc++.so ] || ln -sfv /tools/lib/libstdc++.so{,.6} $LFS/usr/lib
[ -e $LFS/usr/lib/libstdc++.la ] || sed 's/tools/usr/' $LFS/tools/lib/libstdc++.la > $LFS/usr/lib/libstdc++.la
[ -e $LFS/bin/sh ] || ln -sfv bash $LFS/bin/sh

ln -sfv /proc/self/mounts $LFS/etc/mtab

# add user settings
LFS_HOME="/home/lfs"
LINKER_PLATFORM=$(readelf -l /bin/bash | grep interpreter | sed 's/^.*:\(.*\)]/\1/g' | tr -d ' ') && \
echo "host platform linker: $LINKER_PLATFORM found" && \
if [[ $LFS_HOME == "" ]]; then echo "Abort, because wrong home environment value $LFS_HOME"; exit 5; fi && \
mkdir -pv $LFS$LFS_HOME && \
MAKEFLAGS="-j"$(cat /proc/cpuinfo | grep '^processor.*:.*$' | wc -l) && \
if (( $? != 0 )); then MAKEFLAGS="-j1"; fi && \
echo "set +h
umask 022
LINKER_PLATFORM=$LINKER_PLATFORM
MAKEFLAGS=$MAKEFLAGS
LFS_TGT=$LFS_TGT
export LC_ALL LFS_TGT PATH MAKEFLAGS LINKER_PLATFORM
clear
echo \"
Welcome to the LFS change root environment!
See /sources for package archives... Your are now able to build programs for the target system.
Build packages as user lfs and install them as root.

Environment info:
	HOME=\$HOME
	MAKEFLAGS=\$MAKEFLAGS
	LFS_TGT=\$LFS_TGT
	PATH=\$PATH
	LC_ALL=\$LC_ALL
	LINKER_PLATFORM=\$LINKER_PLATFORM
	
\"
cd /sources/pkg && ls *.sh
" > $LFS/root/.bashrc && \
echo "set +h
umask 002
LINKER_PLATFORM=$LINKER_PLATFORM
MAKEFLAGS=$MAKEFLAGS
LFS_TGT=$LFS_TGT
export LC_ALL LFS_TGT PATH MAKEFLAGS LINKER_PLATFORM
clear
echo \"
Welcome to the LFS change root environment!
See /sources for package archives... Your are now able to build programs for the target system.
Build packages as user lfs and install them as root.

Environment info:
	HOME=\$HOME
	MAKEFLAGS=\$MAKEFLAGS
	LFS_TGT=\$LFS_TGT
	PATH=\$PATH
	LC_ALL=\$LC_ALL
	LINKER_PLATFORM=\$LINKER_PLATFORM
	
\"
cd /sources/pkg && ls *.sh
" > $LFS$LFS_HOME/.bashrc && \
echo "exec /tools/bin/env -i \
	HOME=$LFS_HOME \
	TERM="$TERM" \
	PS1='\u:\w$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
	MAKEFLAGS=$MAKEFLAGS \
	LFS_TGT=$LFS_TGT \
	/tools/bin/bash" > $LFS$LFS_HOME/.bash_profile && \
chown -vR lfs.lfs $LFS$LFS_HOME && \
chown -vR lfs $LFS/usr/pkg && \
chown -vR lfs $LFS/sources && \
chown -vR root.root $LFS/tools && \
chown -vR root.root $LFS/etc

# setup users
if [ ! -e $LFS/etc/passwd ]; then
	echo "root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/bin/false
daemon:x:6:6:Daemon User:/dev/null:/bin/false
messagebus:x:18:18:D-Bus Message Daemon User:/var/run/dbus:/bin/false
nobody:x:99:99:Unprivileged User:/dev/null:/bin/false" > $LFS/etc/passwd
	cat /etc/passwd | grep '^[^#]' | grep '^lfs:.*' >> $LFS/etc/passwd
fi

if [ ! -e $LFS/etc/group ]; then
	echo "root:x:0:
bin:x:1:daemon
sys:x:2:
kmem:x:3:
tape:x:4:
tty:x:5:lfs
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
adm:x:16:
messagebus:x:18:
systemd-journal:x:23:
input:x:24:
mail:x:34:
nogroup:x:99:
users:x:999:" > $LFS/etc/group
	cat /etc/group | grep '^[^#]' | grep '^lfs:.*' >> $LFS/etc/group
fi

DUMMYGROUP=$(cat /etc/group | grep '^[a-z]*:[a-z]*:13:' | cut -d: -f1);
if [[ $DUMMYGROUP == "" ]]; then
	echo "No local group with ID 13 found... please add a dummy group with such an ID";
	exit 1;
fi

# activate log files
touch $LFS/var/log/{btmp,lastlog,wtmp}
chgrp -v $DUMMYGROUP $LFS/var/log/lastlog
chmod -v 664  /var/log/lastlog
chmod -v 600  /var/log/btmp

# make an basic hosts file (needed by perl test program)
echo "Check if /etc/hosts exists..."
if [ ! -r $LFS/etc/hosts ]; then
	echo "/etc/hosts missing... create a default one";
	echo "127.0.0.1 localhost $(hostname)" > $LFS/etc/hosts;
fi

# copy resolv.conf into chroot system
if [ ! -e $LFS/etc/resolv.conf.old -a -e $LFS/etc/resolv.conf ]; then
	cp -fv $LFS/etc/resolv.conf $LFS/etc/resolv.conf.old
fi
cp -fv /etc/resolv.conf $LFS/etc/resolv.conf

# enter change root environment
if [ -h $LFS/dev/shm ]; then   mkdir -pv $LFS/; fi && \
chroot "$LFS" /tools/bin/env -i \
	HOME=/root \
	TERM="$TERM" \
	PS1='\u:\w$ ' \
	PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
	MAKEFLAGS=$MAKEFLAGS \
	LFS_TGT=$LFS_TGT \
	/tools/bin/bash

rm $LFS/root/.bashrc;
rm $LFS$LFS_HOME/.bashrc;
$LFS/sources/prepare/unmount_chroot.sh
